//
//  SAAnswerQuestionCollectionViewCell.m
//  SunOn
//
//  Created by RazeenCheng on 2017/5/2.
//  Copyright © 2017年 NeoZhang. All rights reserved.
//

#import "SAAnswerQuestionCollectionViewCell.h"
#import "SAAnswerQuestionTitleCell.h"
#import "SAAnswerQuestionTypeCell.h"
#import "SAAnswerQuestionOptionCell.h"

#import "NSString+BoundingRect.h"

@interface SAAnswerQuestionCollectionViewCell()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, assign) NSInteger index; // 记录当前的进度
@property (nonatomic, assign) NSInteger questionType; // 多选、单选
@property (nonatomic, strong) NSMutableArray *selectedItemArray; // 当前选择的选项

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation SAAnswerQuestionCollectionViewCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}


- (void)configCellWithIndex:(NSInteger)index withArray:(NSArray *)array withSelectItemArray:(NSMutableArray *)selectItemArray
{
    self.index = index;
    self.questionType = [array[1] integerValue];
    self.dataArray = [array mutableCopy];
    self.selectedItemArray = selectItemArray;
    
    [self.tableView reloadData];
}


#pragma mark - 懒加载
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)selectedItemArray
{
    if (!_selectedItemArray) {
        _selectedItemArray = [NSMutableArray array];
    }
    return _selectedItemArray;
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count > 2) {
        if (section == 2) {
            return self.dataArray.count - 2;
        }
        
        return 1;
        
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if (section == 0) {
        SAAnswerQuestionTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAAnswerQuestionTitleCell" forIndexPath:indexPath];
        [cell configCellWithTitle:self.dataArray[0]];
        return cell;
    } else if (section == 1) {
        SAAnswerQuestionTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAAnswerQuestionTypeCell" forIndexPath:indexPath];
        [cell configCellWithQuestionType:[self.dataArray[1] integerValue]];
        return cell;
    } else {
        SAAnswerQuestionOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAAnswerQuestionOptionCell" forIndexPath:indexPath];
        if ([self checkSelectStatusWithIndexPath:indexPath]) {
            [cell configCellWithRow:row withContent:self.dataArray[row + 2] withSelected:YES];
        } else {
            [cell configCellWithRow:row withContent:self.dataArray[row + 2] withSelected:NO];
        }
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    //NSInteger row = indexPath.row;
    
    if (section == 0) {
        NSString *questionTitle = [self.dataArray firstObject];
        CGFloat questionTitleHeight = [questionTitle boundingHeightWithWidth:(SASCREEN_WIDTH - 30) withFont:15];
        CGFloat height = questionTitleHeight + 75;
        return height;
    } else if (section == 1) {
        return 80;
    } else {
        return 87;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        // 选项
        if (self.questionType == 0) {
            // 单选
            SAAnswerQuestionOptionCell *selectCell = [tableView cellForRowAtIndexPath:indexPath];
            if (selectCell.isSelected) {
                // 当前cell之前已经被选中，为取消选中状态的操作
                [selectCell configCellWithSelected:NO];
                [self.selectedItemArray removeObject:indexPath];
            } else {
                // 当前cell之前未被选中，为选中操作
                if (self.selectedItemArray.count > 0) {
                    // 将之前选中的cell置为未选中状态
                    SAAnswerQuestionOptionCell *unSelectCell = [tableView cellForRowAtIndexPath:[self.selectedItemArray firstObject]];
                    [unSelectCell configCellWithSelected:NO];
                    [self.selectedItemArray removeAllObjects];
                }
                
                // 将当前选中的cell置为选中状态
                [selectCell configCellWithSelected:YES];
                [self.selectedItemArray addObject:indexPath];
            }
            
        } else {
            // 多选
            SAAnswerQuestionOptionCell *selectCell = [tableView cellForRowAtIndexPath:indexPath];
            if (selectCell.isSelected) {
                // 当前cell之前已经被选中，为取消选中状态的操作
                [selectCell configCellWithSelected:NO];
                [self.selectedItemArray removeObject:indexPath];
            } else {
                // 当前cell之前未被选中，为选中操作
                [selectCell configCellWithSelected:YES];
                [self.selectedItemArray addObject:indexPath];
            }
        }
    }
}



// 配置选中状态
- (BOOL)checkSelectStatusWithIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedItemArray.count <= 0) {
        return NO;
    }
    
    for (NSIndexPath *selectIndexPath in self.selectedItemArray) {
        if ([indexPath isEqual:selectIndexPath]) {
            return YES;
        }
    }
    return NO;
}

@end
